package cn.dyp.com.splashandguide.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.widget.TextView;

import cn.dyp.com.splashandguide.R;

/**
 * Created by lenovo on 2017/5/20.
 */

public class SplashActivity extends BaseActivity{
    private static final int ENTER_HOME = 0x00;
    private static final int ENTER_SPLASH = 0x01;
    private static final String FILE_NAME = "first";
    private int waitTime = 3 * 1000;
    private TextView mSoftwareVersionTV;
    private SharedPreferences mPrefrence;
    private Handler mHandler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        //第二个参数为对此对象的操作权限，MODE_PRIVATE权限是指只能够被本应用所读写
        mPrefrence = getSharedPreferences(FILE_NAME, MODE_PRIVATE);
        handleMessage();
        initView();
        setText();
        into();
    }

    //消息处理
    private void handleMessage() {
        mHandler = new Handler(new Handler.Callback() {

            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == ENTER_HOME) {
                    // 进入主页面
                    Intent mHomeIntent = new Intent(SplashActivity.this,MainActivity.class);
                    startActivity(mHomeIntent);
                } else if(msg.what == ENTER_SPLASH){
                    // 将App启动的值设为false
                    SharedPreferences.Editor mEditor = mPrefrence.edit();
                    mEditor.putBoolean("isFirst", false);
                    mEditor.commit();
                    // 进入引导界面
                    Intent mSplshIntent = new Intent(SplashActivity.this, GuideActivity.class);
                    startActivity(mSplshIntent);
                }
                finish();
                return false;
            }
        });

    }

    //初始化TextView
    private void initView() {
        mSoftwareVersionTV = (TextView) findViewById(R.id.software_version_tv);
    }

    //给TextView设值
    private void setText() {
        mSoftwareVersionTV.setText(getString(R.string.software_version) + getVersion());
    }

    /**
     * 获取程序的版本号
     * @return 程序版本号
     */
    private String getVersion() {
        String versionString = null;
        try {
            PackageManager manager = getPackageManager();//获取包管理者对象
            PackageInfo info = manager.getPackageInfo(getPackageName(), 0);//获取包的详细信息
            versionString = info.versionName;
            //获取版本号和版本名称
            /*System.out.println("版本号："+info.versionCode);
            System.out.println("版本名称："+info.versionName);*/
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionString;
    }

    // 判断程序是否是第一次运行
    private boolean isFirstRun() {
        //第二个参数默认false，只有参数为"true"是才返回true
        return mPrefrence.getBoolean("isFirst", true);
    }

    // 控制跳转
    private void into() {
        if (isFirstRun()) {
            mHandler.sendEmptyMessageDelayed(ENTER_SPLASH, waitTime);
        } else {
            mHandler.sendEmptyMessageDelayed(ENTER_HOME, waitTime);
        }
    }
}
